package com.ruoyi.report.service.impl;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.report.domain.ReportOrginalimage;
import com.ruoyi.report.domain.ReportOrginalrecord;
import com.ruoyi.report.mapper.ReportOrginalimageMapper;
import com.ruoyi.report.mapper.ReportOrginalrecordMapper;
import com.ruoyi.report.service.IReportOrginalrecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 原始记录信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-10
 */
@Service
public class ReportOrginalrecordServiceImpl implements IReportOrginalrecordService
{
    @Autowired(required = false)
    private ReportOrginalrecordMapper reportOrginalrecordMapper;

    @Autowired(required = false)
    private ReportOrginalimageMapper reportOrginalimageMapper;

    /**
     * 查询原始记录信息
     * 
     * @param id 原始记录信息ID
     * @return 原始记录信息
     */
    @Override
    public ReportOrginalrecord selectReportOrginalrecordById(String id)
    {
        ReportOrginalrecord reportOrginalrecord = reportOrginalrecordMapper.selectReportOrginalrecordById(id);
        reportOrginalrecord.setList(reportOrginalimageMapper.selectReportOrginalimageByOId(id));
        return reportOrginalrecord;
    }

    /**
     * 报告编号查询原始记录
     *
     * @param reportNum
     */
    @Override
    public ReportOrginalrecord selectReportOrginalrecordByReportNum(String reportNum) {
        return reportOrginalrecordMapper.selectReportOrginalrecordByReportNum(reportNum);
    }

    /**
     * 查询原始记录信息列表
     * 
     * @param reportOrginalrecord 原始记录信息
     * @return 原始记录信息
     */
    @Override
    public List<ReportOrginalrecord> selectReportOrginalrecordList(ReportOrginalrecord reportOrginalrecord)
    {
        List<ReportOrginalrecord> reportOrginalrecordList = reportOrginalrecordMapper.selectReportOrginalrecordList(reportOrginalrecord);
        for(ReportOrginalrecord r : reportOrginalrecordList) {
            List<ReportOrginalimage> images = reportOrginalimageMapper.selectReportOrginalimageByOId(r.getId());
            r.setList(images);
            r.setOriginalName(r.getOriginalRecord());
            List<String> profiles = new ArrayList<>();
            for(ReportOrginalimage image : images){
                profiles.add(image.getProfile());
            }
            r.setOriginalRecord(profiles.stream().collect(Collectors.joining(",")));
        }
        return reportOrginalrecordList;
    }

    /**
     * 新增原始记录信息
     * 
     * @param reportOrginalrecord 原始记录信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertReportOrginalrecord(ReportOrginalrecord reportOrginalrecord)
    {
        String id = IdUtils.fastSimpleUUID();
        reportOrginalrecord.setId(id);
        int ret = reportOrginalrecordMapper.insertReportOrginalrecord(reportOrginalrecord);
        if(ret > 0) {
            //新增附件图片数据
            if(null != reportOrginalrecord.getList() && reportOrginalrecord.getList().size() > 0) {
                for(ReportOrginalimage reportOrginalimage : reportOrginalrecord.getList()) {
                    reportOrginalimage.setId(IdUtils.fastSimpleUUID());
                    reportOrginalimage.setoId(id);
                }
                reportOrginalimageMapper.insertBatchReportOrginalimage(reportOrginalrecord.getList());
            }
        }
        return ret;
    }

    /**
     * 批量新增原始记录
     * @param reportOrginalrecord
     * @return
     */
    @Override
    public int insertBatchOrginalrecord(List<ReportOrginalrecord> reportOrginalrecord) {
        return reportOrginalrecordMapper.insertBatchOrginalrecord(reportOrginalrecord);
    }

    /**
     * 修改原始记录信息
     * 
     * @param reportOrginalrecord 原始记录信息
     * @return 结果
     */
    @Override
    public int updateReportOrginalrecord(ReportOrginalrecord reportOrginalrecord)
    {
        return reportOrginalrecordMapper.updateReportOrginalrecord(reportOrginalrecord);
    }

    /**
     * 批量删除原始记录信息
     * 
     * @param ids 需要删除的原始记录信息ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteReportOrginalrecordByIds(String[] ids)
    {
        //删除原始记录信息
        for(int i = 0; i < ids.length; i++) {
            reportOrginalimageMapper.deleteReportOrginalimageByOId(ids[i]);
        }
        return reportOrginalrecordMapper.deleteReportOrginalrecordByIds(ids);
    }

    /**
     * 删除原始记录信息信息
     * 
     * @param id 原始记录信息ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteReportOrginalrecordById(String id)
    {
        reportOrginalimageMapper.deleteReportOrginalimageByOId(id);
        return reportOrginalrecordMapper.deleteReportOrginalrecordById(id);
    }

    /**
     * 根据报告编号删除
     *
     * @param reportNum
     */
    @Override
    public int deleteReportOrginalrecordByReportNum(String reportNum) {
        return reportOrginalrecordMapper.deleteReportOrginalrecordByReportNum(reportNum);
    }
}
